package com.adee.gulimall.gateway.filter;

import lombok.extern.slf4j.Slf4j;
import org.springframework.cloud.gateway.filter.GatewayFilterChain;
import org.springframework.cloud.gateway.filter.GlobalFilter;
import org.springframework.core.Ordered;
import org.springframework.stereotype.Component;
import org.springframework.web.server.ServerWebExchange;
import reactor.core.publisher.Mono;

/**
 * 监控全局过滤器
 * 1.API耗时记录；
 *
 *
 */
@Slf4j
@Component
public class Global_5_MonitorFilter implements GlobalFilter, Ordered {
    @Override
    public Mono<Void> filter(ServerWebExchange exchange, GatewayFilterChain chain) {
        long begin = System.currentTimeMillis();
        Mono<Void> ret = chain.filter(exchange);
        long end = System.currentTimeMillis();
        long millis = end - begin;
        log.info("请求处理耗时：" + millis);
        return ret;
    }

    /**
     * 过滤器的优先级，数字越小优先级越高
     * @return
     */
    @Override
    public int getOrder() {
        return OrderNum.MonitorFilterOrder;
    }
}
